package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.mall.commons.exception.ExceptionProcessorUtils;
import com.cskaoyan.mall.constant.ShoppingRetCode;
import com.cskaoyan.mall.dto.ProductDetailDto;
import com.cskaoyan.mall.dto.ProductDetailRequest;
import com.cskaoyan.mall.dto.ProductDetailResponse;
import com.cskaoyan.mall.dto.ProductDto;
import com.cskaoyan.shopping.converter.ProductConverter;
import com.cskaoyan.shopping.dal.entitys.Item;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dto.AllProductRequest;
import com.cskaoyan.shopping.dto.AllProductResponse;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.dto.RecommendResponse;
import com.cskaoyan.shopping.service.IProductService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashSet;
import java.util.List;
import java.util.Set;

/**
 * @author lihaoming
 * @Description
 * @Create 2022-07-08 23:18
 * @Version
 */
@Service
public class IProductServiceImpl implements IProductService {
  @Autowired
  ItemMapper itemMapper;
  @Autowired
  ProductConverter productConverter;



    @Override
    public ProductDetailResponse getProductDetail(ProductDetailRequest request) {
        ProductDetailResponse response = new ProductDetailResponse();
        try {
            // 参数校验
            request.requestCheck();
            Item item = itemMapper.selectByPrimaryKey(request.getId());
            ProductDetailDto productDetailDto = productConverter.item2DetailDto(item);
            productDetailDto.setProductImageSmall(item.getImages());

            //执行成功
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
            response.setProductDetailDto(productDetailDto);


        } catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response ;
    }

    @Override
    public AllProductResponse getAllProduct(AllProductRequest request) {
        AllProductResponse response = new AllProductResponse();
        //TODO:在业务层利用返回的item的do转换为dto并封装为response，返回给controller层
        try {
            // 参数校验
            request.requestCheck();
            //返回数据库对应的item，利用转换器转换
            //排序方式
            String orderDir = request.getSort();
            //排序字段
            String orderCol=null;
            if(orderDir==null||"".equals(orderDir)){
                orderDir="desc";
                orderCol="created";

            }
            if("1".equals(orderDir)){
                orderDir="asc";
                orderCol="price";
            }
            if("-1".equals(orderDir)){
                orderDir="desc";
                orderCol="price";
            }
            PageHelper.startPage(request.getPage(), request.getSize());

            List<Item> items = itemMapper.selectItemFront(orderCol,orderDir,request.getPriceGt(),request.getPriceLte(),request.getCid()
                    );
            PageInfo<Item> pageInfo = new PageInfo<>(items);
            //将do转换为dto，并返回response
            List<ProductDto> productDto = productConverter.items2Dto(items);
            response.setProductDtoList(productDto);
            response.setTotal(pageInfo.getTotal());
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());

        }catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }

        return response;
    }




    @Override
    public RecommendResponse getRecommendGoods() {
        RecommendResponse response = new RecommendResponse();
        try {
            // 参数校验
            Panel panel = itemMapper.getRecommendGoods();
            PanelDto panelDto = productConverter.panel2PanelDto(panel);
            // response.setPanelContentItems(panelDto);
            Set<PanelDto> panelDtoSet=new HashSet<>();
            panelDtoSet.add(panelDto);
            response.setPanelContentItemDtos(panelDtoSet);
            response.setCode(ShoppingRetCode.SUCCESS.getCode());
            response.setMsg(ShoppingRetCode.SUCCESS.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            ExceptionProcessorUtils.wrapperHandlerException(response, e);
        }
        return response;

    }

    // @Override
    // public AllItemResponse getAllItems() {
    //     return null;
    // }
}
